import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class ComposeView1500LazyPage extends StatefulWidget {
  const ComposeView1500LazyPage({super.key});

  @override
  State<ComposeView1500LazyPage> createState() => _ComposeView1500LazyPageState();
}

class _ComposeView1500LazyPageState extends State<ComposeView1500LazyPage> {
  final DateTime _start = DateTime.now();
  final int totalCount = 1500;
  bool _renderReported = false;

  @override
  void initState() {
    super.initState();

    // 注册首帧回调
    SchedulerBinding.instance.addTimingsCallback(_onFrameTimings);
  }

  void _onFrameTimings(List<FrameTiming> timings) {
    if (_renderReported) return;
    _renderReported = true;

    final duration = DateTime.now().difference(_start).inMilliseconds;
    debugPrint("🕒 Flutter Lazy 页面 首帧渲染完成耗时：$duration ms");

    // 取消监听（只记录一次）
    SchedulerBinding.instance.removeTimingsCallback(_onFrameTimings);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("Flutter ComposeView1500 Lazy")),
      body: ListView.builder(
        itemCount: totalCount,
        itemBuilder: (context, index) {
          return Container(
            height: 100,
            margin: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
            decoration: BoxDecoration(
              border: Border.all(color: Colors.red, width: 2),
            ),
            alignment: Alignment.center,
            child: Text("Item #$index"),
          );
        },
      ),
    );
  }
}
